package com.apexwear.apexutils.service;

import android.annotation.SuppressLint;
import android.app.LocaleManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.apex.bluetooth.callback.BleConnectStatusListener;
import com.apex.bluetooth.callback.DeviceBugCallback;
import com.apex.bluetooth.callback.GeneralCallback;
import com.apex.bluetooth.callback.OtaCallback;
import com.apex.bluetooth.callback.WatchInfoCallback;
import com.apex.bluetooth.core.EABleBluetoothOption;
import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.QueryWatchInfoType;
import com.apex.bluetooth.model.EABleBindInfo;
import com.apex.bluetooth.model.EABleDev;
import com.apex.bluetooth.model.EABleOta;
import com.apex.bluetooth.model.EABleReadDebug;
import com.apex.bluetooth.model.EABleWatchInfo;
import com.apex.jieli.ota.JieliOtaInstance;
import com.apex.jieli.watcha.JieliWatchFaceManager;
import com.apexwear.apexutils.common.Constant;
import com.apexwear.apexutils.model.BinFileInfo;
import com.apexwear.apexutils.model.Ota;
import com.apexwear.apexutils.utils.FileUtils;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class BluetoothService extends Service {
    final String TAG = this.getClass().getSimpleName();
    final IBinder binder = new MyBinder();
    ConnectListener connectListener;
    private volatile boolean isConnect;
    ReceiveOtaInfo receiveOtaInfo;
    int watchType;

    int currentProgress;
    Handler mHandler = new Handler() {
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x08) {
                final Ota otaInfo = (Ota) msg.obj;
                final EABleOta eaBleOta = new EABleOta();
                eaBleOta.setFilePath(otaInfo.getFilePath());
                eaBleOta.setPop(true);
                eaBleOta.setWatchType(watchType);
                String type = otaInfo.getFileType();
                if (type.equalsIgnoreCase("Res")) {
                    eaBleOta.setOtaType(EABleOta.OtaType.res);
                } else if (type.equalsIgnoreCase("Apollo")) {
                    //  if (otaInfo.getWatchType().equalsIgnoreCase("002086")) {
                    if (watchType == 4) {//判断是否为双备份
                        eaBleOta.setOtaType(EABleOta.OtaType.jieLi_707);
                    } else {
                        eaBleOta.setOtaType(EABleOta.OtaType.apollo);
                    }
                    //   } else {
                    //       eaBleOta.setOtaType(EABleOta.OtaType.apollo);
                    //   }
                } else if (type.equalsIgnoreCase("Hr")) {
                    eaBleOta.setOtaType(EABleOta.OtaType.hr);
                } else if (type.equalsIgnoreCase("Tp")) {
                    eaBleOta.setOtaType(EABleOta.OtaType.tp);
                } else if (type.equalsIgnoreCase("Dial")) {
                    if (otaInfo.getWatchType() == "001061") {
                        eaBleOta.setOtaType(EABleOta.OtaType.his_watchface);
                    } else {
                        eaBleOta.setOtaType(EABleOta.OtaType.user_wf);
                    }
                } else if (type.equalsIgnoreCase("Smt")) {
                    eaBleOta.setOtaType(EABleOta.OtaType.stm32);
                } else if (type.equalsIgnoreCase("Gps")) {
                    eaBleOta.setOtaType(EABleOta.OtaType.gps);
                } else if (type.equalsIgnoreCase("his_json")) {
                    eaBleOta.setOtaType(EABleOta.OtaType.his_json);
                }
                eaBleOta.setVersion(otaInfo.getOtaVersion());
                //判断是否为表盘

                if (eaBleOta.getOtaType() == EABleOta.OtaType.user_wf || eaBleOta.getOtaType() == EABleOta.OtaType.his_watchface) {
                    //如果为表盘,则判断是否已有表盘ID
                    String path = eaBleOta.getFilePath();
                    if (TextUtils.isEmpty(path) || "".equalsIgnoreCase(path) || !new File(path).exists()) {
                        Log.e(TAG, "ota表盘时参数错误");
                        sendOtaResultState(false);
                        return;
                    }
                    if (eaBleOta.getOtaType() == EABleOta.OtaType.user_wf) {
                        if (watchType == 4 || watchType == 5) {
                            JieliWatchFaceManager.getInstance().addWatchFace(eaBleOta.getFilePath(), new OtaCallback() {
                                @Override
                                public void success() {
                                    sendOtaResultState(true);
                                }

                                @Override
                                public void progress(int i) {
                                    Log.i(TAG, "当前进度：" + i);
                                    if (i != currentProgress) {
                                        currentProgress = i;
                                        sendProgress(i, false);
                                    }
                                }

                                @Override
                                public void hisUpdateId(int[] ints) {

                                }

                                @Override
                                public void mutualFail(int i) {
                                    sendOtaResultState(false);
                                }
                            });
                            return;
                        }
                        handleDial(eaBleOta);
                    } else {
                        Message message = new Message();
                        message.obj = eaBleOta;
                        message.what = 0x09;
                        mHandler.sendMessage(message);
                    }
                } else {
                    if (eaBleOta.getOtaType() != EABleOta.OtaType.jieLi_707) {

                        List<EABleOta> otaList = new ArrayList<>();
                        otaList.add(eaBleOta);
                        if (isConnect) {
                            currentProgress = 0;
                            EABleManager.getInstance().otaUpdate(otaList, new OtaCallback() {
                                @Override
                                public void success() {
                                    sendOtaResultState(true);
                                }

                                @Override
                                public void progress(int i) {
                                    EABleOta.OtaType otaType = otaList.get(0).getOtaType();
                                    if (otaType != null && otaType == EABleOta.OtaType.his_json) {
                                        return;
                                    }
                                    if (i != currentProgress) {
                                        currentProgress = i;
                                        sendProgress(i, false);
                                    }


                                }

                                @Override
                                public void hisUpdateId(int[] ints) {
                                    Log.e(TAG, "当前需要更新的id:" + Arrays.toString(ints));
                                    new Thread() {
                                        @Override
                                        public void run() {
                                            super.run();
                                            try {
                                                List<EABleOta> oList = getHisUpdateFileInfo(eaBleOta.getFilePath(), ints);
                                                if (oList != null && !oList.isEmpty()) {
                                                    if (isConnect) {
                                                        currentProgress = 0;
                                                        EABleManager.getInstance().otaUpdate(oList, new OtaCallback() {
                                                            @Override
                                                            public void success() {
                                                                sendOtaResultState(true);
                                                            }

                                                            @Override
                                                            public void progress(int i) {
                                                                if (i != currentProgress) {
                                                                    currentProgress = i;
                                                                    sendProgress(i, false);
                                                                }
                                                            }

                                                            @Override
                                                            public void hisUpdateId(int[] ints) {

                                                            }

                                                            @Override
                                                            public void mutualFail(int i) {
                                                                sendOtaResultState(false);
                                                            }
                                                        });
                                                        return;
                                                    }
                                                }
                                            } catch (IOException e) {
                                                Log.e(TAG, "获取his文件时出现异常:" + e.getMessage());
                                            }
                                            sendOtaResultState(false);
                                        }
                                    }.start();
                                }

                                @Override
                                public void mutualFail(int i) {
                                    Log.e(TAG, "错误码:" + i);
                                    sendOtaResultState(false);


                                }
                            });
                        }
                    } else {
                        if (isConnect) {
                            currentProgress = 0;
                            EABleDev eaBleDev = new EABleDev();
                            eaBleDev.setE_ops(EABleDev.DevOps.jl707_ota_start_request);
                            EABleManager.getInstance().setDeviceOps(eaBleDev, new GeneralCallback() {
                                @Override
                                public void result(boolean b, int i) {
                                    if (b) {
                                        new Timer().schedule(new TimerTask() {
                                            @Override
                                            public void run() {
                                                JieliOtaInstance.getInstance().startOta(eaBleOta.getFilePath(), new OtaCallback() {
                                                    @Override
                                                    public void success() {
                                                        sendOtaResultState(true);
                                                    }

                                                    @Override
                                                    public void progress(int i) {
                                                        Log.i(TAG, "当前进度：" + i);
                                                        if (i != currentProgress) {
                                                            currentProgress = i;
                                                            sendProgress(i, true);
                                                        }
                                                    }

                                                    @Override
                                                    public void hisUpdateId(int[] ints) {

                                                    }

                                                    @Override
                                                    public void mutualFail(int i) {
                                                        Log.e(TAG, "错误码:" + i);
                                                        sendOtaResultState(false);
                                                    }
                                                }, BluetoothService.this);
                                            }
                                        }, 2000);

                                    } else {
                                        Log.e(TAG, "请求707开始OTA错误:" + i);
                                        sendOtaResultState(false);
                                    }
                                }

                                @Override
                                public void mutualFail(int i) {
                                    Log.e(TAG, "请求707OTA开始错误:" + i);
                                    sendOtaResultState(false);
                                }
                            });


                        }
                    }


                }
            } else if (msg.what == 0x09) {
                if (isConnect) {
                    EABleOta eaBleOta = (EABleOta) msg.obj;
                    List<EABleOta> otaList = new ArrayList<>();
                    otaList.add(eaBleOta);
                    currentProgress = 0;
                    EABleManager.getInstance().otaUpdate(otaList, new OtaCallback() {
                        @Override
                        public void success() {
                            sendOtaResultState(true);
                        }

                        @Override
                        public void progress(int i) {
                            if (i != currentProgress) {
                                currentProgress = i;
                                sendProgress(i, false);
                            }
                        }

                        @Override
                        public void hisUpdateId(int[] ints) {

                        }

                        @Override
                        public void mutualFail(int i) {
                            Log.e(TAG, "错误码:" + i);
                            sendOtaResultState(false);


                        }
                    });
                }
            }
        }
    };

    private List<EABleOta> getHisUpdateFileInfo(String jsonFilePath, final int[] ids) throws IOException {
        if (TextUtils.isEmpty(jsonFilePath) || ids == null) {
            return null;
        }
        final File jFile = new File(jsonFilePath);
        if (jFile == null || !jFile.exists()) {
            return null;
        }
        FileInputStream fileInputStream = new FileInputStream(jFile);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String tempString;
        StringBuilder readJson = new StringBuilder();
        while ((tempString = bufferedReader.readLine()) != null) {
            readJson.append(tempString);
        }
        inputStreamReader.close();
        bufferedReader.close();
        JsonParser jsonParser = new JsonParser();
        JsonObject jsonObject = jsonParser.parse(readJson.toString()).getAsJsonObject();
        String hisVersion = null;
        List<EABleOta> otaList = null;
        if (jsonObject != null) {
            Map<String, BinFileInfo> fileInfoMap = new HashMap<>();
            for (String key : jsonObject.keySet()) {
                BinFileInfo binFileInfo = new BinFileInfo();
                if ("Version".equalsIgnoreCase(key)) {
                    binFileInfo.Version = jsonObject.get(key).getAsString();
                    hisVersion = binFileInfo.Version;
                } else if ("IdMax".equalsIgnoreCase(key)) {
                    continue;
                } else {
                    binFileInfo.path = jsonObject.get(key).getAsJsonObject().get("path").getAsString();
                    binFileInfo.md5 = jsonObject.get(key).getAsJsonObject().get("md5").getAsString();
                }
                fileInfoMap.put(key, binFileInfo);
            }
            if (!fileInfoMap.isEmpty()) {
                otaList = new ArrayList<>();
                for (int resId : ids) {
                    BinFileInfo binFileInfo = fileInfoMap.get(resId + "");
                    if (binFileInfo != null) {
                        String filePath = jFile.getParent() + "/" + binFileInfo.path;
                        Log.e(TAG, "文件最终地址:" + filePath);
                        if (!TextUtils.isEmpty(filePath)) {
                            File hisFile = new File(filePath);
                            if (hisFile != null && hisFile.exists()) {
                                EABleOta eaBleOta = new EABleOta();
                                eaBleOta.setOtaType(EABleOta.OtaType.his);
                                eaBleOta.setVersion(hisVersion);
                                eaBleOta.setFilePath(filePath);
                                eaBleOta.setPop(true);
                                eaBleOta.setHis_res_id(resId);

                                otaList.add(eaBleOta);
                            }
                        }

                    }
                }
            }
        }
        return otaList;

    }

    private byte[] file2Bytes(@NonNull File f) {
        byte[] buffer = null;
        try {
            FileInputStream fis = new FileInputStream(f);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

    private void handleDial(final EABleOta eaBleOta) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                String originalPath = eaBleOta.getFilePath();
                File file = new File(originalPath);
                byte[] fByte = file2Bytes(file);
                if (fByte == null || fByte.length <= 0) {
                    sendOtaResultState(false);
                    return;
                }
                byte[] dialId = new byte[32];
                System.arraycopy(fByte, 108, dialId, 0, dialId.length);
                boolean exitId = false;
                for (int i = 0; i < dialId.length; i++) {
                    if (dialId[i] != 0) {
                        exitId = true;
                        break;
                    }
                    Log.e(TAG, "正在判断在线表盘的ID");
                }
                if (!exitId) {
                    String wId = String.valueOf(System.currentTimeMillis());
                    byte[] idByte = wId.getBytes(Charset.forName("UTF-8"));
                    int len;
                    if (idByte.length > 32) {
                        len = 32;
                    } else {
                        len = idByte.length;
                    }
                    for (int i = 0; i < len; ++i) {
                        dialId[i] = idByte[i];
                    }
                    Log.e(TAG, "走到这里....");
                    System.arraycopy(dialId, 0, fByte, 108, dialId.length);

                    //形成新的文件
                    String nFilePath = FileUtils.getDownPath(BluetoothService.this);
                    String lastName = originalPath.substring(originalPath.lastIndexOf("/") + 1, originalPath.length());
                    nFilePath += ("/" + lastName);
                    Log.e(TAG, "新生成的文件地址:" + nFilePath);
                    //  nFilePath.replaceAll(name, "id_" + name);
                    File nFile = new File(nFilePath);
                    if (nFile.exists() && nFile.isFile()) {
                        nFile.delete();
                    }
                    boolean result = saveByte2File(fByte, nFilePath);
                    if (!result) {
                        //开始传输数据给手表
                        Log.e(TAG, "改变表盘ID失败");
                        sendOtaResultState(false);
                        return;
                    } else {
                        eaBleOta.setFilePath(nFilePath);
                    }
                } else {
                    eaBleOta.setFilePath(originalPath);
                }
                if (mHandler != null) {
                    Message message = new Message();
                    message.obj = eaBleOta;
                    message.what = 0x09;
                    mHandler.sendMessage(message);
                }
            }
        }.start();
    }

    private boolean saveByte2File(byte[] bytes, String path) {
        if (bytes == null || bytes.length <= 0 || TextUtils.isEmpty(path) || "".equalsIgnoreCase(path)) {
            return false;
        }

        try {
            FileOutputStream fos = new FileOutputStream(new File(path), true);
            fos.write(bytes);
            fos.flush();
            fos.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public class MyBinder extends Binder {
        public BluetoothService getService() {
            return BluetoothService.this;    //返回本服务
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (receiveOtaInfo == null) {
            receiveOtaInfo = new ReceiveOtaInfo();
            IntentFilter intentFilter = new IntentFilter(Constant.CONNECT_STATE_BROADCAST);
            intentFilter.addAction(Constant.START_UPDATE_BROADCAST);
            LocalBroadcastManager.getInstance(this).registerReceiver(receiveOtaInfo, intentFilter);

        }
    }

    public void onLowMemory() {
        super.onLowMemory();
        Log.e(TAG, "onLowMemory()");
        onDestroy();

    }

    public void connectWatch(String macAddress) {
        if (TextUtils.isEmpty(macAddress)) {
            return;
        }
        if (connectListener == null) {
            connectListener = new ConnectListener();
        }
        EABleBluetoothOption.isTestVersion = true;
        try {
            Log.i(TAG, "链接蓝牙设备");
            EABleManager.getInstance().connectToPeripheral(macAddress, BluetoothService.this, connectListener, 400, null, null, true);
        } catch (Exception e) {

        }
    }

    /**
     * 解绑设备
     */
    public void unbindWatch() {
        if (isConnect) {
            isConnect = false;
            EABleManager.getInstance().disconnectPeripheral();
            watchType = 0;
            sendUnbindState(true);

        }
    }

    @Override
    public void onDestroy() {
        if (receiveOtaInfo != null) {
            LocalBroadcastManager.getInstance(this).unregisterReceiver(receiveOtaInfo);
            receiveOtaInfo = null;
        }
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        EABleManager.getInstance().disconnectPeripheral();
        connectListener = null;
        super.onDestroy();
    }

    public void entryOrExitMode(final boolean isEntry) {
        if (isConnect) {
            EABleDev eaBleDev = new EABleDev();
            if (isEntry) {
                eaBleDev.setE_ops(EABleDev.DevOps.enter_factory_test_mode);
            } else {
                eaBleDev.setE_ops(EABleDev.DevOps.exit_factory_test_mode);
            }
            EABleManager.getInstance().setDeviceOps(eaBleDev, new GeneralCallback() {
                @Override
                public void result(boolean b, int i) {
                    if (isEntry) {
                        setResultState(b, Constant.ENTRY_TEST_STATE_BROADCAST);
                    } else {
                        setResultState(b, Constant.EXIT_TEST_STATE_BROADCAST);
                    }
                }

                @Override
                public void mutualFail(int i) {
                    if (isEntry) {
                        setResultState(false, Constant.ENTRY_TEST_STATE_BROADCAST);
                    } else {
                        setResultState(false, Constant.EXIT_TEST_STATE_BROADCAST);
                    }
                }
            });
        }
    }

    public void printLog(EABleReadDebug eaBleDebug, DeviceBugCallback debugLogCallback) {
        if (isConnect && eaBleDebug != null) {
            Log.e(TAG, "获取调试信息");
            EABleManager.getInstance().getWatchDebugInfo(eaBleDebug, debugLogCallback);
        }

    }

    class ConnectListener implements BleConnectStatusListener {

        @Override
        public void deviceConnected() {
            isConnect = true;
            sendConnectState(true);
            //打开手表屏幕
            Log.i(TAG, "开打开屏幕");
            EABleBindInfo eaBleBindInfo = new EABleBindInfo();
            eaBleBindInfo.setE_ops(EABleBindInfo.BindingOps.end);
            EABleManager.getInstance().setOpsBinding(eaBleBindInfo, new GeneralCallback() {
                @Override
                public void result(boolean b, int i) {
                    Log.e(TAG, "结果:" + b + ",值:" + i);


                }

                @Override
                public void mutualFail(int i) {
                    Log.e(TAG, "失败:" + i);
                }
            });
            Log.i(TAG, "获取手表信息");
            EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.watch_info, new WatchInfoCallback() {
                @Override
                public void watchInfo(EABleWatchInfo eaBleWatchInfo) {
                    if (eaBleWatchInfo != null) {
                        watchType = eaBleWatchInfo.getLcd_pixel_type();
                        if (eaBleWatchInfo.getLcd_pixel_type() == 4 || eaBleWatchInfo.getLcd_pixel_type() == 5) {
                            JieliWatchFaceManager.getInstance().release();
                            JieliWatchFaceManager.getInstance().addReceiveDeviceData();
                            JieliWatchFaceManager.getInstance().addConnectListener();
                            JieliWatchFaceManager.getInstance().init();

                        }
                    }

                }

                @Override
                public void mutualFail(int i) {

                }
            });


        }

        @Override
        public void deviceDisconnect() {
            isConnect = false;
            sendConnectState(false);
            watchType = 0;

        }

        @Override
        public void deviceNotFind() {
            isConnect = false;
            sendConnectState(false);
            watchType = 0;

        }

        @Override
        public void unopenedBluetooth() {
            isConnect = false;
            sendConnectState(false);
            watchType = 0;

        }

        @Override
        public void connectError(int i) {
            isConnect = false;
            sendConnectState(false);
            watchType = 0;
        }

        @Override
        public void unsupportedBLE() {
            isConnect = false;
            sendConnectState(false);
            watchType = 0;

        }

        @Override
        public void notOpenLocation() {
            isConnect = false;
            sendConnectState(false);
            watchType = 0;

        }

        @Override
        public void connectTimeOut() {
            isConnect = false;
            sendConnectState(false);
            watchType = 0;

        }
    }

    private void sendConnectState(boolean state) {
        Intent intent = new Intent(Constant.CONNECT_STATE_BROADCAST);
        intent.putExtra("connect_state", state);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

    private void sendUnbindState(boolean state) {
        Intent intent = new Intent(Constant.UNBIND_BROADCAST);
        intent.putExtra("unbind_state", state);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }


    private void setResultState(boolean state, String action) {
        Intent intent = new Intent(action);
        intent.putExtra("result_state", state);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

    private void sendOtaResultState(boolean state) {
        Intent intent = new Intent(Constant.OTA_RESULT_BROADCAST);
        intent.putExtra("result_state", state);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

    private void sendProgress(int progress, boolean is707) {
        Intent intent = new Intent(Constant.OTA_PROGRESS_BROADCAST);
        intent.putExtra("progressValue", progress);
        intent.putExtra("deviceType", is707);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

    public void changeMac(String mac) {
        if (TextUtils.isEmpty(mac)) {
            return;
        }
        if (isConnect) {
            EABleWatchInfo eaBleWatchInfo = new EABleWatchInfo();
            eaBleWatchInfo.setWatchId("888888888888" + mac);
            Log.e(TAG, "id:" + eaBleWatchInfo.getWatchId() + "id长度:" + eaBleWatchInfo.getWatchId().length());
            EABleManager.getInstance().setMacAddress(eaBleWatchInfo, new GeneralCallback() {
                @Override
                public void result(boolean b, int i) {
                    Log.e(TAG, "改号状态:" + (b ? "成功" : "失败"));
                    setResultState(b, Constant.CHANGE_MAC_STATE_BROADCAST);
                }

                @Override
                public void mutualFail(int i) {
                    Log.e(TAG, "改号失败");
                    setResultState(false, Constant.CHANGE_MAC_STATE_BROADCAST);

                }
            });

        }
    }

    public boolean isConnect() {
        return isConnect;
    }

    class ReceiveOtaInfo extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (!TextUtils.isEmpty(action) && action.equalsIgnoreCase(Constant.START_UPDATE_BROADCAST)) {
                Ota otaInfo = (Ota) intent.getSerializableExtra("ota_info");
                if (otaInfo == null) {
                    sendOtaResultState(false);
                } else {
                    if (mHandler != null) {
                        Message message = new Message();
                        message.obj = otaInfo;
                        message.what = 0x08;
                        mHandler.sendMessage(message);
                    }


                }
            }
        }
    }
}
